﻿using EmitMapper;
using EmitMapper.MappingConfiguration;
using LiteExpress.Demo.Business.Models;
using LiteExpress.Demo.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteExpress.Demo.Business.Dto
{
    public static class UserMapper
    {
        static ObjectsMapper<UserEntity, UserModel> map_d2m;//entity to model
        static ObjectsMapper<UserModel, UserEntity> map_m2d;//model to entity

        /// <summary>
        /// initialize mapper
        /// </summary>
        static UserMapper()
        {
            #region entity to model
            map_d2m = ObjectMapperManager.DefaultInstance.GetMapper<UserEntity, UserModel>(
                new DefaultMapConfig().MatchMembers((x, y) =>
                {
                    if (x == "UserID" && y == "Id")
                    {
                        return true;
                    }

                    if (x == "UserName" && y == "Name")
                    {
                        return true;
                    }
                    return x == y;
                }));
            #endregion

            #region model to entity
            map_m2d = ObjectMapperManager.DefaultInstance.GetMapper<UserModel, UserEntity>(
                new DefaultMapConfig().MatchMembers((x, y) =>
                {
                    if (x == "Id" && y == "UserID")
                    {
                        return true;
                    }

                    if (x == "Name" && y == "UserName")
                    {
                        return true;
                    }
                    return x == y;
                }));
            #endregion
        }


        public static UserEntity ToEntity(this UserModel model)
        {
            return map_m2d.Map(model);
        }

        public static UserModel ToModel(this UserEntity entity)
        {
            return map_d2m.Map(entity);
        }
    }
}
